<?php

namespace framework\system\manager;

use framework;
use framework\system\kernel\Manager;

class SessionManager extends Manager {

    public $session_id = null;

    public function init() {
        
    }

    public function startSession() {
        if (!isset($_SESSION)) {
            session_start();
        }
        $this->session_id = framework::cm()->get("com.request")->get("cookie.PHPSESSID");
        framework::mm()->send("message.session.id", $this->session_id);
        if (!empty($_SESSION)) {
            foreach ($_SESSION as $key => $value) {

                framework::mm()->send("message.session.{$key}", $value);
            }
        }
    }

    public function getId() {
        return $this->session_id;
    }

    public function get($name) {

        $value = "";
        if (array_key_exists($name, $_SESSION)) {
            $value = $_SESSION[$name];
        } else {
            $value = FALSE;
        }
        return $value;
    }

    public function set($name, $value) {
        if (!empty($name)) {
            $_SESSION[$name] = $value;
        }
    }

    public function end() {
        $this->startSession();
        $_SESSION = [];
        session_unset();
        session_destroy();
    }

}
